package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 通过分类id查询套餐
     * @param setmealDTO
     * @return
     */
    @Override
    public List<Setmeal> getSetmealByCategoryId(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        List<Setmeal> setmealList = setmealMapper.getSetmeal(setmeal);
        return setmealList;
    }

    /**
     * 根据套餐id查询详细菜品
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getSetmealDishBySetmealId(Long id) {
        List<DishItemVO> setmealDishList = setmealDishMapper.getDishItemVOBySetmealId(id);
        return setmealDishList;
    }

    /**
     * 通过id查询套餐
     * @param id
     * @return
     */
    @Override
    public SetmealVO getSetmealAndDishById(Long id) {
        Setmeal setmeal = setmealMapper.getSetmealById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        List<SetmealDish> setmealDishList = setmealDishMapper.getSetmealDishBySetmealId(setmeal.getId());
        setmealVO.setSetmealDishes(setmealDishList);
        return setmealVO;
    }

    /**
     * 分页查询套餐
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult getSetmealByPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealPageQueryDTO, setmeal);
        log.info("setmeal:{}", setmeal);
        List<SetmealVO> setmealVOList = setmealMapper.getSetmealVO(setmeal);
        Page<SetmealVO> setmealVOPage = (Page<SetmealVO>) setmealVOList;
        return new PageResult(setmealVOPage.getTotal(), setmealVOPage.getResult());
    }

    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @Override
    @Transactional
    public void addSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.addSetmeal(setmeal);
        Long id = setmeal.getId();

        List<SetmealDish> setmealDishList = setmealDTO.getSetmealDishes();
        if(setmealDishList != null && setmealDishList.size() > 0) {
            for (SetmealDish setmealDish : setmealDishList) {
                setmealDish.setSetmealId(id);
                setmealDishMapper.addSetmealDish(setmealDish);
            }
        }
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void updateSetmealAndDish(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        //判断是否有未起售的菜品
        int count = dishMapper.countDishByStatusAndIds(setmealDTO.getId(), StatusConstant.DISABLE);
        Integer status = setmealMapper.getStatusById(setmealDTO.getId());
//        log.info("old_status:{}", status.equals(StatusConstant.DISABLE));
//        log.info("new_status:{}", setmealVO.getStatus().equals(StatusConstant.ENABLE));
//        log.info("count: {}", count);
        if(count > 0 && status.equals(StatusConstant.DISABLE) && setmealDTO.getStatus().equals(StatusConstant.ENABLE)) {
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
        }

        setmealMapper.updateSetmeal(setmeal);

        List<SetmealDish> setmealDishList = setmealDTO.getSetmealDishes();
        if(setmealDishList != null && setmealDishList.size() > 0) {
            setmealDishMapper.deleteSetmealDish(setmealDTO.getId());
            for (SetmealDish setmealDish : setmealDishList) {
                setmealDish.setSetmealId(setmeal.getId());
                setmealDishMapper.addSetmealDish(setmealDish);
            }
        }
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public void deleteSetmeal(List<Long> ids) {

        //判断是否都是未起售状态
        int count = setmealMapper.countSetmealByStatusAndIds(ids, StatusConstant.ENABLE);
        if(count > 0) {
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }

        ids.forEach(id ->{
            setmealDishMapper.deleteSetmealDish(id);
        });
        setmealMapper.deleteSetmeal(ids);
    }
}
